package com.zrrd.yunchmall.user.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zrrd.yunchmall.pojo.UserTmp;
import com.zrrd.yunchmall.user.mapper.UserMapper;
import com.zrrd.yunchmall.user.util.JwtUtil;
import org.springframework.security.crypto.bcrypt.BCrypt;
import org.springframework.stereotype.Service;

@Service
public class UserServiceImpl  extends ServiceImpl<UserMapper, UserTmp> {
    /*执行登录认证的方法
    @param username用户名
    @param password 密码
    @return 认证通过则返回jwt令牌，失败返回null
     */

    public String login(String username,String password){
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("username",username);
        //select uid,username,password...from t_user where username=?
        UserTmp userTmp = getOne(queryWrapper);
        //userTmp ==null 说明用户名不存在
        if (userTmp !=null){
          if (userTmp.getPassword().equals(password));
          if (BCrypt.checkpw(password,userTmp.getPassword())){
           return JwtUtil.create(30*60*1000,userTmp);
          }
        }
        //用户名或密码错误返回null
        return null;
    }
}
